package at.codebase.db.entities;

import com.google.appengine.api.blobstore.BlobKey;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;

@Entity
public class EImage {

	@Id
	private Long imageId;
	@Index
	private Key<EUser> uploader;
	private Key<EItem> item;
	private Key<EUser> user;
	
	private long timeCreate;
	
	private int imageWidth;
	private int imageHeight;
	
	private String imageBlobKey;

	/*
	 * Objectify
	 */
	public static Key<EImage> key(Long imageId) {
		return Key.create(EImage.class, imageId.longValue());
	}

	public static Ref<EImage> ref(EImage image) {
		Key<EImage> key = key(image.getId());
		return Ref.create(key);
	}

	/*
	 * Constructors
	 */
	public EImage() {
		
	}

	public EImage(Key<EUser> uploader, Key<EItem> item, Key<EUser> user, long timeCreate, int imageWidth, int imageHeight, String imageBlobKey) {
		super();
		this.uploader = uploader;
		this.item = item;
		this.user = user;
		this.timeCreate = timeCreate;
		this.imageWidth = imageWidth;
		this.imageHeight = imageHeight;
		this.imageBlobKey = imageBlobKey;
	}
	
	public static EImage createItemImage(Key<EUser> uploader, Key<EItem> item, BlobKey imageBlob, int imageWidth, int imageHeight){
		return new EImage(uploader, item, null, System.currentTimeMillis(), imageWidth, imageHeight, imageBlob.getKeyString());
	}
	
	public static EImage createUserImage(Key<EUser> user, BlobKey imageBlob, int imageWidth, int imageHeight){
		return new EImage(user, null, user, System.currentTimeMillis(), imageWidth, imageHeight, imageBlob.getKeyString());
	}
	
	public Long getId() {
		return imageId;
	}

	public void setId(Long imageId) {
		this.imageId = imageId;
	}

	public Key<EUser> getUploader() {
		return uploader;
	}

	public void setUploader(Key<EUser> uploader) {
		this.uploader = uploader;
	}

	public Key<EItem> getItem() {
		return item;
	}

	public void setItem(Key<EItem> item) {
		this.item = item;
	}

	public Long getImageId() {
		return imageId;
	}

	public void setImageId(Long imageId) {
		this.imageId = imageId;
	}

	public Key<EUser> getUser() {
		return user;
	}

	public void setUser(Key<EUser> user) {
		this.user = user;
	}

	public long getTimeCreate() {
		return timeCreate;
	}

	public void setTimeCreate(long timeCreate) {
		this.timeCreate = timeCreate;
	}

	public int getImageWidth() {
		return imageWidth;
	}

	public void setImageWidth(int imageWidth) {
		this.imageWidth = imageWidth;
	}

	public int getImageHeight() {
		return imageHeight;
	}

	public void setImageHeight(int imageHeight) {
		this.imageHeight = imageHeight;
	}

	public String getImageBlobKey() {
		return imageBlobKey;
	}

	public void setImageBlobKey(String imageBlobKey) {
		this.imageBlobKey = imageBlobKey;
	}

	@Override
	public String toString() {
		return "EImage [imageId=" + imageId + ", uploader=" + uploader
				+ ", item=" + item + ", user=" + user + ", timeCreate="
				+ timeCreate + ", imageWidth=" + imageWidth + ", imageHeight="
				+ imageHeight + ", imageBlobKey=" + imageBlobKey + "]";
	}
	
}
